package com.yuxin.common.security;

import com.yuxin.common.util.AntPathMatcher;
import com.yuxin.common.util.StringUtils;
import com.yuxin.site.ConfigService;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.List;

/**
 * @author zehui.zeng
 * @date 13-8-29 下午5:44
 */
public class ResourceAccessService {

    /***
     * 判断是否有权限访问
     * @param userType
     * @param contextPath
     * @param resource
     * @return
     */
    public static boolean hasResPermission(Map user,String contextPath,String resource){
        Map permConfig = ConfigService.getWZPermConfig();
        List<String> resKeyList = new ArrayList();
        resKeyList.add("anonymous");
        if(user != null){
            String userType = StringUtils.getText(user.get("TYPE"));
            // 添加版本权限key
            if(StringUtils.hasText(userType) && !StringUtils.hasText(userType.replaceAll("\\d",""))){
                resKeyList.add("user-"+userType);
            }
        }
        AntPathMatcher pathMatcher = new AntPathMatcher();
        Map permMap = null;
        Map includeMap = null;
        Map excludeMap = null;
        Iterator includeIte = null;
        Iterator excludeIte = null;
        boolean hasPermission = false;
        for(String resKey : resKeyList){
            permMap = (Map)permConfig.get(resKey);
            includeMap = (Map)permMap.get("include");
            excludeMap = (Map)permMap.get("exclude");
            includeIte = includeMap.keySet().iterator();
            // 判断包含权限
            while (includeIte.hasNext()){
                String key = includeIte.next()+"";
                //System.out.println(contextPath+includeMap.get(key)+"----"+resource+"---"+pathMatcher.match(contextPath+includeMap.get(key),resource));
                if(pathMatcher.match(contextPath+includeMap.get(key),resource)){
                    hasPermission = true;
                    break;
                }
            }
            // 判断包含除外的权限
            excludeIte = excludeMap.keySet().iterator();
            while (excludeIte.hasNext()){
                String key = excludeIte.next()+"";
                //System.out.println(contextPath+excludeMap.get(key)+"--5--"+resource+"---"+pathMatcher.match(contextPath+excludeMap.get(key),resource));
                if(pathMatcher.match(contextPath+excludeMap.get(key),resource)){
                    hasPermission = false;
                    break;
                }
            }
        }
        return hasPermission;
    }

    /***
     * 是否有功能权限
     * @param user
     * @param contextPath
     * @param resource
     * @return
     */
    public static boolean hasFunPermission(Map user,String funKey){
        if(user == null){
            return false;
        }
        Map permConfig = ConfigService.getWZPermConfig();
        List<String> resKeyList = new ArrayList();
        resKeyList.add("anonymous");
        if(user != null){
            String userType = StringUtils.getText(user.get("TYPE"));
            // 添加版本权限key,站点类型必须为数字
            if(!StringUtils.hasText(userType.replaceAll("\\d",""))){
                resKeyList.add("user-"+userType);
            }
        }
        Map permMap = null;
        for(String resKey : resKeyList){
            permMap = (Map)permConfig.get(resKey);
            if(permMap != null && permMap.get("fun") != null){
                List funList = (List)permMap.get("fun");
                // 判断包含权限
                if(funList.contains(funKey)){
                    return true;
                }
            }
        }
        return false;
    }

}
